﻿using Model.Config;
using SqlSugar;

namespace Webapi.Extensions
{
    public static class ServiceExtension
    {
        public static IServiceCollection AddSqlSugar(this IServiceCollection services, IConfiguration configuration,out bool status)
        {
            status = true;
            var Dblist = configuration.GetSection("Db").Get<List<Dbconfig>>();
            if (Dblist == null) {
                Console.WriteLine("数据库配置信息读取失败");
                status = false;
                return services; 
            }

            List<ConnectionConfig> list = new List<ConnectionConfig>();

            for (int i = 0; i < Dblist.Count; i++)
            {
                list.Add(new ConnectionConfig()
                {
                    ConfigId = Dblist[i].Id,
                    ConnectionString = Dblist[i].ConnectionString,
                    DbType = (DbType)Enum.Parse(typeof(DbType), Dblist[i].Type),
                    IsAutoCloseConnection = true
                });
            }
            services.AddScoped<ISqlSugarClient>(provider =>
            {
                var db = new SqlSugarClient(list);
                return db;
            });



            return services;
        }
    }
}
